/* eslint-disable no-extra-parens */
import { InitModalMethod, ParseModalMethod } from './defination';

/**
 * 设置表单属性
 * @param { Object } form 表单数据
 * @param { Object } workstep cached的ai工作流
 */
export const initModalData = (form = {}, workstep = {}, tts = false) => {
	if (!form || !workstep) {
		return;
	}
	console.log(workstep, 'interfaceCallInitModalData', form);
	if (workstep) {
		form.informationRushurlAddress = workstep.configurationParameter.url;
		form.waitTime = workstep.configurationParameter.timeout;
		form.callMethod = workstep.configurationParameter.method;
		if (workstep.configurationParameter.requestBody) {
			form.requestBody = workstep.configurationParameter.requestBody || [];
		}
		if (workstep.configurationParameter.responseBody) {
			form.responseBody = workstep.configurationParameter.responseBody || [];
		}
	}

	// yes
	if (workstep.answer[0]?.do) {
		form.nextStepYes = workstep.answer[0]?.do?.id;
		// form.nextStepYes = workstep.answer[0]?.do?.no;
	}

	// errorRemind
	if (workstep.errorRemind?.do) {
		form.nextStepOther = workstep.errorRemind?.do?.id;
		// form.nextStepOther = workstep.errorRemind?.do?.no;
		form.otherTimesMax = workstep.errorRemind?.maximum; // 4

		form.name = workstep.name;
	};
};
InitModalMethod['interfaceCall'] = initModalData;
/**
 * 解析表单属性
 * @param { Object } form 表单数据
 * @param { Object } workstep cached的ai工作流
 */
export const parseModalData = (form = {}, workstep = {}, tts = false) => {
	// console.log(form, workstep);
	if (!form || !workstep) {
		return;
	}
	if (workstep) {
		workstep.configurationParameter.url = form.informationRushurlAddress;
		workstep.configurationParameter.timeout = form.waitTime;
		workstep.configurationParameter.method = form.callMethod;
		if (form.requestBody) {
			workstep.configurationParameter.requestBody = form.requestBody || [];
		}
		if (form.responseBody) {
			workstep.configurationParameter.responseBody = form.responseBody || [];
		}
		if (form.callMethod === 'get') {
			workstep.configurationParameter.requestBody = []
		}
		if (form.callMethod === 'delete') {
			workstep.configurationParameter.responseBody = []
		}


		if (workstep.answer) {
			workstep.answer[0].do.id = form.nextStepYes;
			workstep.answer[0].do.no = form.nextStepYes;
		}
		if (workstep.errorRemind) {
			workstep.errorRemind.do.id = form.nextStepOther;
			workstep.errorRemind.do.no = form.nextStepOther;
			workstep.errorRemind.maximum = form.otherTimesMax;
		}
	}

	/** 变量存储处理结束 */

	workstep.name = form.name;
	console.log(form.responseBody, 'responseBody', form.requestBody, 'configurationParameterParseModalData', workstep.configurationParameter);
};
ParseModalMethod['interfaceCall'] = parseModalData;

/* interfaceCall: {
	configurationParameter: {// 接口调用的配置参数
		url: '',
		timeout: 0, // 单位秒
		method: 'GET', // GET  PUT  POST DELETE
		requestBody: [// body  PUT  POST DELETE有这些参数， GET没有requestBody
			{
				fieldName: '',// 字段名
				parameterName: ''// 参数名 NULL
			},
			{
				fieldName: '',// 字段名
				parameterName: ''// 参数名
			}
		],
		responseBody: [
			{
				fieldName: 'ge',// 字段名
				parameterName: '',// 参数名
				defaultValue: null
			},
			{
				fieldName: '',// 字段名
				parameterName: '',// 参数名
				defaultValue: null
			}
		]
	},
	type: 'interfaceCall',
	do: {
		command: 'toCallingInterface'
	},
	answer: [
		{
			do: {
				id: '',
				no: '',
				type: 'yes',
				command: 'jump-to'
			},
			key: 'yes',
			type: 'yes'
		},
		{
			do: {
				command: 'toCallingInterface'
			},
			key: 'no',
			type: 'no'
		}
	],
	errorRemind: {
		do: {
			id: '',
			no: '',
			type: 'no',
			command: 'jump-to'
		},
		maximum: 0
	}
} */
